package Server;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;


public class ServerView {

	private JFrame frmServerChat;
	private JTextArea textChat;
	private JScrollPane chatPane;
	private JPanel connectionPanel;
	private JLabel lblPort;
	private JTextField textPort;
	private JButton btnConnect;
	private JButton btnDisconnect;
	
	private Server server;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ServerView window = new ServerView();
					window.frmServerChat.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public ServerView() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmServerChat = new JFrame();
		frmServerChat.setTitle("Server Chat");
		frmServerChat.setResizable(false);
		frmServerChat.setBounds(100, 100, 800, 200);
		frmServerChat.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		frmServerChat.getContentPane().setLayout(null);
		
		textChat = new JTextArea();
		textChat.setLineWrap(true);
		textChat.setEditable(false);
		
		chatPane = new JScrollPane(textChat);
		chatPane.setBounds(20, 20, 594, 130);
		frmServerChat.getContentPane().add(chatPane);
		
		connectionPanel = new JPanel();
		connectionPanel.setBounds(594, 0, 200, 572);
		frmServerChat.getContentPane().add(connectionPanel);
		connectionPanel.setLayout(null);
		
		lblPort = new JLabel("Port:");
		lblPort.setBounds(40, 40, 40, 20);
		connectionPanel.add(lblPort);
		
		textPort = new JTextField();
		textPort.setText("2000");
		textPort.setBounds(90, 40, 100, 20);
		connectionPanel.add(textPort);
		textPort.setColumns(10);
		
		btnConnect = new JButton("Connect");
		btnConnect.setBounds(90, 80, 100, 25);
		connectionPanel.add(btnConnect);
		
		btnDisconnect = new JButton("Disconnect");
		btnDisconnect.setEnabled(false);
		btnDisconnect.setBounds(90, 120, 100, 25);
		connectionPanel.add(btnDisconnect);
		
		frmServerChat.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				if (btnDisconnect.isEnabled()) {
					disconnect();
				}
				frmServerChat.dispose();
			}
		});
		
		btnConnect.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				try {
					int port = Integer.parseInt(textPort.getText());
					if (port > 1023) {
						connect(port);
					} else {
						JOptionPane.showMessageDialog(null, "Invalid port", "Connection error", JOptionPane.ERROR_MESSAGE);
					}
				} catch (NumberFormatException e) {
					JOptionPane.showMessageDialog(null, "NumberFormatException", "Connection error", JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		
		btnDisconnect.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				disconnect();
			}
		});
	}
	
	public void connect(int port) {
		try {
			this.server = new Server(this, port);
			this.server.start();
			btnConnect.setEnabled(false);
			btnDisconnect.setEnabled(true);
			textChat.setText("");
			textPort.setEnabled(false);
			this.server.log("Servidor conectado\n");
		} catch (IOException e) {
			JOptionPane.showMessageDialog(null, "IOException", "Connection error", JOptionPane.ERROR_MESSAGE);
		}
	}
	
	@SuppressWarnings("deprecation")
	public void disconnect() {
		try {
			this.server.interrupt();
			this.server.stopConnection();
			this.server.stop();
		} catch (IOException e) {
			JOptionPane.showMessageDialog(null, "IOException", "Connection error", JOptionPane.ERROR_MESSAGE);
		}
		btnConnect.setEnabled(true);
		btnDisconnect.setEnabled(false);
		textPort.setEnabled(true);
	}
	
	public void print(String message) {
		this.textChat.append(message);
		JScrollBar sb = this.chatPane.getVerticalScrollBar();
		sb.setValue(sb.getMaximum());
	}
}
